package cn.ad.server.web.utils;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.scheduling.annotation.Async;

/** 
 * 请求性能监控
 * @author Wu Chengjun
 * @date 2016年5月9日 上午12:20:47 
 * @since 1.7 
 */
public class PerformenceMonitor {
	
	private static Logger LOGGER = LogManager.getLogger("monitor");
	
	/**
	 * 是否开启
	 */
	private boolean enable;
	
	/**
	 * 检测方法执行
	 * @param call
	 * @throws Throwable
	 */
	public Object doAround(ProceedingJoinPoint call) throws Throwable {
		Object result = null;
        long inittime = System.nanoTime();
        result = call.proceed();  
        double processTime = (System.nanoTime() - inittime) * 0.000001;
        if(processTime > 50){
        	log(call, processTime);
        }
        return result;  
	}
	
	@Async
	public void log(ProceedingJoinPoint call, double processTime){
		LOGGER.info("[{}] {} {} - {}" , Thread.currentThread().getName(), call.getTarget().getClass(), call.getSignature().getName(),  processTime);
	}

	/**
	 * @return the enable
	 */
	public boolean isEnable() {
		return enable;
	}

	/**
	 * @param enable the enable to set
	 */
	public void setEnable(boolean enable) {
		this.enable = enable;
	}
}
